package com.xiang.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 *  SimpleChannelInboundHandler extends ChannelInboundHandlerAdapter
 *      1、指定数据类型
 *      2、自动释放byteBuf资源
 */
@Slf4j
@ChannelHandler.Sharable //pipeline中实例共享标识
public class ServerSimpleInbandHandler extends SimpleChannelInboundHandler<ByteBuf> {
    int n =0;
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        n++;
        byte[] bytes =new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        String result = new String(bytes, StandardCharsets.UTF_8);
        log.info("读取条数：{} --> 数据：{}",n,result);
    }
}
